layui.use(['table', 'laytpl', 'layer'], function() {
	var url = location.href;
	var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
	var id = paraString[0].substring(paraString[0].indexOf("=") + 1, paraString[0].length);
	var type = paraString[1].substring(paraString[1].indexOf("=") + 1, paraString[1].length);
	var $ = layui.$;
	if(type == 'edit') {
		getrole();
		noreadonly();
		employeeMsg();
	} else if(type=='detail') {
		getrole();
		readonly();
		employeeMsg();
	}else{
		getrole();
	}
	function getrole() {
		$.ajax({
			type: "post",
			url: "/getroletree",
			async: true,
			success: function(data) {
				var result = JSON.parse(data.data);
				var sele = $("#rolename");
				for(var i = 0; i < result.length; i++) {
					var options = "<option value=" + result[i].rolename + ">" + result[i].rolename + "</option>";
					sele.append($(options));
				}
				
			}
		});
	}

	function employeeMsg() {
		$.ajax({
			type: "post",
			url: "/employeemsg",
			async: true,
			data: {
				id: id
			},
			success: function(data) {
				var ret = JSON.parse(data);
				$(".username").val(ret.username);
				$(".realname").val(ret.realname);
				$("#rolename").val(ret.rolename);
				$(".email").val(ret.email);
				$(".phone").val(ret.phone);
				var options=$("option");
				for(var i=0;i<options.length;i++){
					var name=$(options[i]).val();
					if(name==ret.rolename){
						$(options[i]).attr("selected","selected");
						$(options[i]).siblings().removeAttr("selected");
					}
				}
                
			}
		});
	}

	function readonly() {
		$(".username").attr("readonly", "readonly");
		$(".realname").attr("readonly", "readonly");
		$(".rolename").attr("readonly", "readonly");
		$(".email").attr("readonly", "readonly");
		$(".phone").attr("readonly", "readonly");
	}

	function noreadonly() {
		$(".username").removeAttr("readonly");
		$(".rolename").removeAttr("readonly");
		$("realname").removeAttr("readonly");
		$(".email").removeAttr("readonly");
		$(".phone").removeAttr("readonly");
	}

});